package com.pu.controller.admin;

import com.pu.dto.OrdersCancelDTO;
import com.pu.dto.OrdersConfirmDTO;
import com.pu.dto.OrdersPageQueryDTO;
import com.pu.dto.OrdersRejectionDTO;
import com.pu.result.PageResult;
import com.pu.result.Result;
import com.pu.service.OrderService;
import com.pu.vo.OrderStatisticsVO;
import com.pu.vo.OrderVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @Author Yusc
 * @Date 2023/11/4 21:27
 * @Version
 * @Description 商家端订单管理
 **/
@RestController("adminOrderController")
@RequestMapping("/admin/order")
@Slf4j
@Api(tags = "订单管理接口")
public class OrderController {
    @Autowired
    private OrderService orderService;

    /**
     * @param ordersPageQueryDTO 前端传递的数据
     * @auther: Yusc
     * @date 2023/11/4 21:28
     * @Description 管理端-订单分页搜索
     */
    @GetMapping("/conditionSearch")
    @ApiOperation("订单搜索")
    public Result<PageResult> conditionSearch(OrdersPageQueryDTO ordersPageQueryDTO) {
        log.info("订单查询:{}", ordersPageQueryDTO);
        PageResult pageResult = orderService.conditionSearch(ordersPageQueryDTO);
        return Result.success(pageResult);
    }

    /**
     * @auther: Yusc
     * @date 2023/11/4 21:47
     * @Description 各个状态的订单数量统计
     */
    @GetMapping("/statistics")
    @ApiOperation("各个状态的订单数量统计")
    public Result<OrderStatisticsVO> statistics() {
        OrderStatisticsVO orderStatisticsVO = orderService.statistics();
        return Result.success(orderStatisticsVO);
    }

    /**
     * @param id 订单id
     * @auther: Yusc
     * @date 2023/11/4 10:30
     * @Description 订单详情
     */
    @GetMapping("/details/{id}")
    @ApiOperation("查询订单详情")
    public Result<OrderVO> details(@PathVariable("id") Long id) {
        OrderVO orderVO = orderService.details(id);
        return Result.success(orderVO);
    }

    /**
     * @auther: Yusc
     * @date 2023/11/5 0:10
     * @Description 接单
     * 商家接单其实就是将订单的状态修改为“已接单”
     */
    @PutMapping("/confirm")
    @ApiOperation("接单")
    public Result confirm(@RequestBody OrdersConfirmDTO ordersConfirmDTO) {
        log.info("商家接单:{}", ordersConfirmDTO);
        orderService.confirm(ordersConfirmDTO);
        return Result.success();
    }

    /**
     * @auther: Yusc
     * @date 2023/11/5 11:16
     * @Description 拒单
     */
    @PutMapping("/rejection")
    @ApiOperation("拒单")
    public Result rejection(@RequestBody OrdersRejectionDTO ordersRejectionDTO) throws Exception {
        log.info("拒绝接单：{}", ordersRejectionDTO);
        orderService.rejection(ordersRejectionDTO);
        return Result.success();
    }

    /**
     * @auther: Yusc
     * @date 2023/11/5 11:30
     * @Description 取消订单
     */
    @PutMapping("/cancel")
    @ApiOperation("取消订单")
    public Result cancel(@RequestBody OrdersCancelDTO ordersCancelDTO) throws Exception {
        log.info("取消订单：{}", ordersCancelDTO);
        orderService.cancel(ordersCancelDTO);
        return Result.success();
    }

    /**
     * @auther: Yusc
     * @date 2023/11/5 11:41
     * @Description 派送订单
     */
    @PutMapping("/delivery/{id}")
    @ApiOperation("派送订单")
    public Result delivery(@PathVariable("id") Long id) {
        log.info("派送订单：{}", id);
        orderService.delivery(id);
        return Result.success();
    }

    /**
     * @auther: Yusc
     * @date 2023/11/5 11:56
     * @Description 完成订单
     */
    @PutMapping("/complete/{id}")
    @ApiOperation("完成订单")
    public Result complete(@PathVariable("id") Long id) {
        log.info("完成订单:{}", id);
        orderService.complete(id);
        return Result.success();
    }

}
